package com.basic.business.kanban.controller;

import com.basic.business.actionPoint.service.ITActionPointService;
import com.basic.business.goodsIndicators.service.ITGoodsIndicatorsService;
import com.basic.business.kanban.vo.*;
import com.basic.business.linkBacklog.service.ITLinkBacklogService;
import com.basic.common.annotation.FileExport;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * 链接管理看板
 */
@RestController
@RequestMapping("/business/linkManageBoard")
public class LinkManageBoardController extends BaseController {

    @Resource
    private ITGoodsIndicatorsService tGoodsIndicatorsService;

    @Resource
    private ITActionPointService tActionPointService;

    @Resource
    private ITLinkBacklogService tLinkBacklogService;

    /**
     * 获取连接管理数据
     * @param linkManagePageReqVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:linkManageBoard:list')")
    @GetMapping("/list")
    public TableDataInfo listLinkManage(LinkManagePageReqVo linkManagePageReqVo) {
        startPage();
        List<LinkManageRespVo> list = tGoodsIndicatorsService.getLinkData(linkManagePageReqVo);

        //只有所有指标标准都不为空才能匹配行动点
        if (linkManagePageReqVo.getPayAmount()!=null&&linkManagePageReqVo.getPayBuyerNumber()!=null&&linkManagePageReqVo.getSearchVisitorNumber()!=null&&linkManagePageReqVo.getGoodsPayConversionRatio()!=null){
            for (LinkManageRespVo linkManageRespVo : list){
                //匹配行动点
                Optional.ofNullable(tActionPointService.getActionPoint(linkManageRespVo))
                        .ifPresent(actionPoint -> {
                            linkManageRespVo.setJudgeType(actionPoint.getJudgeType());
                            linkManageRespVo.setActionPoint(actionPoint.getActionPoint());
                        });

                //获取商品ID近30天生成的待办事项数量
                int backLogCount = tLinkBacklogService.getBackLogCountByGoodsId(linkManageRespVo.getGoodsId());
                linkManageRespVo.setBackLogCount(backLogCount);
            }
        }
        return getDataTable(list);

    }

    /**
     * 获取链接待办复盘数据
     * @param linkReplayPageReqVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:linkReplay:list')")
    @GetMapping("/linkReplayData")
    public TableDataInfo linkReplayData(LinkReplayPageReqVo linkReplayPageReqVo) {
        startPage();

        //已优化记录
        List<LinkReplayRespVo> optimizeList = tLinkBacklogService.getOptimizeList(linkReplayPageReqVo);

        return getDataTable(optimizeList);
    }

    /**
     * 获取链接复盘汇总数据
     * @param linkReplayPageReqVo
     * @return
     */
    @GetMapping("/getReplayCollectData")
    @PreAuthorize("@ss.hasPermi('business:linkReplay:collectData')")
    public AjaxResult getReplayCollectData(LinkReplayPageReqVo linkReplayPageReqVo) {
        LinkManageCollectRespVo list = tLinkBacklogService.getLinkManageCollectData(linkReplayPageReqVo);

        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('business:linkReplay:export')")
    @FileExport(fileName = "链接复盘数据.xlsx")
    @PostMapping("/exportLinkReplayData")
    public String exportLinkReplayData(@RequestBody LinkReplayPageReqVo linkReplayPageReqVo) throws IOException {

        List<LinkReplayRespVo> list = tLinkBacklogService.getOptimizeList(linkReplayPageReqVo);

        return EasyExcelUtils.excelExport(LinkReplayRespVo.class,"链接复盘数据", list);
    }
}
